#!/bin/bash

#
#  Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements.  The ASF licenses this file to You
#  under the Apache License, Version 2.0 (the "License"); you may not
#  use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.  For additional information regarding
#  copyright in this work, please see the NOTICE file in the top level
#  directory of this distribution.
#

set -e # exit immediately if a simple command exits with a non-zero status
set -u # report the usage of uninitialized variables

# Setup env vars and folders
RUN_DIR=/var/vcap/sys/run/docker-bosh-elasticsearch_docker
LOG_DIR=/var/vcap/sys/log/docker-bosh-elasticsearch_docker
PACKAGES_DIR=/var/vcap/packages/elasticsearch_docker

PIDFILE=$RUN_DIR/docker-bosh-elasticsearch_docker.pid

source /var/vcap/packages/common/utils.sh

case $1 in

  start)
    pid_guard $PIDFILE docker-bosh-elasticsearch_docker

    mkdir -p $RUN_DIR
    mkdir -p $LOG_DIR

    chown vcap:vcap $RUN_DIR
    chown -R vcap:vcap $LOG_DIR

    echo $$ > $PIDFILE
    chown vcap:vcap $PIDFILE

    for image in $PACKAGES_DIR/*.tgz; do
      /var/vcap/packages/docker/bin/docker \
        --host unix:///var/vcap/sys/run/docker/docker.sock \
        load -i $image \
        >>$LOG_DIR/docker-bosh-elasticsearch_docker.stdout.log \
        2>>$LOG_DIR/docker-bosh-elasticsearch_docker.stderr.log
    done

    # do nothing forever
    exec tail -f /dev/null
    ;;

  stop)
    kill_and_wait $PIDFILE

    ;;
  *)
    echo "Usage: docker-bosh-elasticsearch_docker_ctl {start|stop}"

    ;;

esac
exit 0